#include <sdk.h>
#include <wx/filefn.h>
#include "filepathpanel.h"

const long FilePathPanel::ID_STATICTEXT1 = wxNewId();
const long FilePathPanel::ID_STATICTEXT2 = wxNewId();
const long FilePathPanel::ID_TEXTCTRL1 = wxNewId();
const long FilePathPanel::ID_BUTTON1 = wxNewId();
const long FilePathPanel::ID_STATICTEXT3 = wxNewId();
const long FilePathPanel::ID_TEXTCTRL2 = wxNewId();
const long FilePathPanel::ID_CHECKBOX1 = wxNewId();
const long FilePathPanel::ID_STATICTEXT4 = wxNewId();
const long FilePathPanel::ID_CHECKLISTBOX2 = wxNewId();
const long FilePathPanel::ID_BUTTON2 = wxNewId();
const long FilePathPanel::ID_BUTTON3 = wxNewId();

BEGIN_EVENT_TABLE( FilePathPanel, wxPanel )


END_EVENT_TABLE()

FilePathPanel::FilePathPanel( wxWindow* parent, wxWindowID id ) :
  m_Selection( -1 ) {
  wxStaticText* StaticText1;
  wxStaticText* StaticText4;
  wxBoxSizer* BoxSizer1;
  wxStaticText* StaticText2;
  Create( parent, id, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T( "id" ) );
  BoxSizer1 = new wxBoxSizer( wxVERTICAL );
  StaticText1 = new wxStaticText( this, ID_STATICTEXT1, _T( "Please enter the file\'s location and name and\nwhether to add it to the active project." ), wxDefaultPosition, wxDefaultSize, 0, _T( "ID_STATICTEXT1" ) );
  BoxSizer1->Add( StaticText1, 0, wxALL | wxALIGN_LEFT, 8 );
  StaticText2 = new wxStaticText( this, ID_STATICTEXT2, _T( "Filename with full path:" ), wxDefaultPosition, wxDefaultSize, 0, _T( "ID_STATICTEXT2" ) );
  BoxSizer1->Add( StaticText2, 0, wxTOP | wxLEFT | wxRIGHT | wxEXPAND, 8 );
  BoxSizer2 = new wxBoxSizer( wxHORIZONTAL );
  txtFilename = new wxTextCtrl( this, ID_TEXTCTRL1, _T( "Text" ), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T( "ID_TEXTCTRL1" ) );
  BoxSizer2->Add( txtFilename, 1, wxALL | wxALIGN_TOP, 0 );
  btnBrowse = new wxButton( this, ID_BUTTON1, _T( "..." ), wxDefaultPosition, wxSize( 22, 22 ), 0, wxDefaultValidator, _T( "ID_BUTTON1" ) );
  BoxSizer2->Add( btnBrowse, 0, wxALL | wxALIGN_TOP, 0 );
  BoxSizer1->Add( BoxSizer2, 0, wxBOTTOM | wxLEFT | wxRIGHT | wxEXPAND, 8 );
  lblGuard = new wxStaticText( this, ID_STATICTEXT3, _T( "Header guard word:" ), wxDefaultPosition, wxDefaultSize, 0, _T( "ID_STATICTEXT3" ) );
  BoxSizer1->Add( lblGuard, 0, wxTOP | wxLEFT | wxRIGHT | wxEXPAND, 8 );
  txtGuard = new wxTextCtrl( this, ID_TEXTCTRL2, _T( "Text" ), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T( "ID_TEXTCTRL2" ) );
  BoxSizer1->Add( txtGuard, 0, wxBOTTOM | wxLEFT | wxRIGHT | wxEXPAND, 8 );
  BoxSizer1->Add( -1, -1, 0, wxALL | wxEXPAND, 0 );
  chkAddToProject = new wxCheckBox( this, ID_CHECKBOX1, _T( "Add file to active project" ), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T( "ID_CHECKBOX1" ) );
  chkAddToProject->SetValue( false );
  BoxSizer1->Add( chkAddToProject, 0, wxTOP | wxLEFT | wxRIGHT | wxEXPAND, 8 );
  FlexGridSizer1 = new wxFlexGridSizer( 0, 2, 0, 0 );
  FlexGridSizer1->AddGrowableCol( 1 );
  FlexGridSizer1->AddGrowableRow( 1 );
  FlexGridSizer1->Add( 16, 16, 0, wxALL | wxALIGN_TOP, 0 );
  StaticText4 = new wxStaticText( this, ID_STATICTEXT4, _T( "In build target(s):" ), wxDefaultPosition, wxDefaultSize, 0, _T( "ID_STATICTEXT4" ) );
  FlexGridSizer1->Add( StaticText4, 0, wxALL | wxEXPAND, 0 );
  FlexGridSizer1->Add( 16, 16, 0, wxALL | wxALIGN_TOP, 0 );
  clbTargets = new wxCheckListBox( this, ID_CHECKLISTBOX2, wxDefaultPosition, wxSize( 222, 111 ), 0, 0, wxLB_SINGLE | wxLB_HSCROLL, wxDefaultValidator, _T( "ID_CHECKLISTBOX2" ) );
  clbTargets->Disable();
  FlexGridSizer1->Add( clbTargets, 1, wxALL | wxEXPAND | wxFIXED_MINSIZE, 0 );
  FlexGridSizer1->Add( 16, 16, 0, wxALL | wxALIGN_TOP, 0 );
  BoxSizer6 = new wxBoxSizer( wxHORIZONTAL );
  btnAll = new wxButton( this, ID_BUTTON2, _T( "All" ), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T( "ID_BUTTON2" ) );
  btnAll->Disable();
  BoxSizer6->Add( btnAll, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, 4 );
  btnNone = new wxButton( this, ID_BUTTON3, _T( "None" ), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T( "ID_BUTTON3" ) );
  btnNone->Disable();
  BoxSizer6->Add( btnNone, 0, wxALIGN_CENTER_VERTICAL, 0 );
  FlexGridSizer1->Add( BoxSizer6, 0, wxALL | wxALIGN_TOP, 0 );
  BoxSizer1->Add( FlexGridSizer1, 1, wxBOTTOM | wxLEFT | wxRIGHT | wxEXPAND, 8 );
  BoxSizer3 = new wxBoxSizer( wxVERTICAL );
  BoxSizer1->Add( BoxSizer3, 0, wxBOTTOM | wxLEFT | wxRIGHT | wxEXPAND, 8 );
  SetSizer( BoxSizer1 );
  BoxSizer1->Fit( this );
  BoxSizer1->SetSizeHints( this );
  Connect( ID_TEXTCTRL1, wxEVT_COMMAND_TEXT_UPDATED, ( wxObjectEventFunction )&FilePathPanel::OntxtFilenameText );
  Connect( ID_BUTTON1, wxEVT_COMMAND_BUTTON_CLICKED, ( wxObjectEventFunction )&FilePathPanel::OnbtnBrowseClick );
  Connect( ID_CHECKBOX1, wxEVT_COMMAND_CHECKBOX_CLICKED, ( wxObjectEventFunction )&FilePathPanel::OnchkAddToProjectChange );
  Connect( ID_BUTTON2, wxEVT_COMMAND_BUTTON_CLICKED, ( wxObjectEventFunction )&FilePathPanel::OnbtnAllClick );
  Connect( ID_BUTTON3, wxEVT_COMMAND_BUTTON_CLICKED, ( wxObjectEventFunction )&FilePathPanel::OnbtnNoneClick );
  txtFilename->SetValue( wxEmptyString );
  txtGuard->SetValue( wxEmptyString );
  M_Code_Project* prj = Manager::Get()->GetProjectManager()->GetActiveProject();
  if( prj ) {
    clbTargets->Clear();
    for( int i = 0; i < prj->GetBuildTargetsCount(); ++i ) {
      ProjectBuildTarget* bt = prj->GetBuildTarget( i );
      if( bt ) {
        clbTargets->Append( bt->GetTitle() );
      }
    }
    clbTargets->SetSelection( clbTargets->FindString( prj->GetActiveBuildTarget() ) );
    chkAddToProject->SetValue( true );
    ToggleVisibility( true );
  } else {
    chkAddToProject->SetValue( false );
    ToggleVisibility( false );
  }
}

FilePathPanel::~FilePathPanel() {
}

void FilePathPanel::SetAddToProject( bool add ) {
  M_Code_Project* prj = Manager::Get()->GetProjectManager()->GetActiveProject();
  add = add && prj;
  chkAddToProject->SetValue( add );
  ToggleVisibility( add );
}


int FilePathPanel::GetTargetIndex() {
  int selection = -1;
  if( m_Selection > static_cast<int>( clbTargets->GetCount() ) - 2 ) {
    m_Selection = -1;
    return m_Selection;
  }
  for( selection = m_Selection + 1; selection < static_cast<int>( clbTargets->GetCount() ); ++selection ) {
    if( clbTargets->IsChecked( selection ) ) {
      break;
    }
  }
  m_Selection = selection;
  return m_Selection;
}

void FilePathPanel::OntxtFilenameText( M_Code_unused wxCommandEvent& event ) {
  if( !txtFilename || txtFilename->GetValue().IsEmpty() ) {
    return;
  }
  wxString name = wxFileNameFromPath( txtFilename->GetValue() );
  while( name.Replace( _T( " " ), _T( "_" ) ) )
    ;
  while( name.Replace( _T( "\t" ), _T( "_" ) ) )
    ;
  while( name.Replace( _T( "." ), _T( "_" ) ) )
    ;
  name.MakeUpper();
  name << _T( "_INCLUDED" );
  txtGuard->SetValue( name );
}

void FilePathPanel::OnbtnBrowseClick( M_Code_unused wxCommandEvent& event ) {
  M_Code_Project* prj = Manager::Get()->GetProjectManager()->GetActiveProject();
  wxFileDialog dlg( this,
                    _T( "Select filename" ),
                    prj ? prj->GetBasePath() : _T( "" ),
                    txtFilename->GetValue(),
                    m_ExtFilter,
                    wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
  PlaceWindow( &dlg );
  if( dlg.ShowModal() == wxID_OK ) {
    txtFilename->SetValue( dlg.GetPath() );
  }
}

void FilePathPanel::OnchkAddToProjectChange( wxCommandEvent& event ) {
  SetAddToProject( event.IsChecked() );
}

void FilePathPanel::OnbtnAllClick( M_Code_unused wxCommandEvent& event ) {
  for( int i = 0; i < static_cast<int>( clbTargets->GetCount() ); ++i ) {
    clbTargets->Check( i, true );
  }
}

void FilePathPanel::OnbtnNoneClick( M_Code_unused wxCommandEvent& event ) {
  for( int i = 0; i < static_cast<int>( clbTargets->GetCount() ); ++i ) {
    clbTargets->Check( i, false );
  }
}

void FilePathPanel::ToggleVisibility( bool on ) {
  clbTargets->Enable( on );
  btnAll->Enable( on );
  btnNone->Enable( on );
}
